package com.leapp.sk.viewdelegate.login

import android.support.v7.app.AppCompatActivity
import android.text.Editable
import android.text.TextUtils
import android.text.TextWatcher
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import cc.vv.lkbasecomponent.util.LKRegexUtil
import com.example.baselibrary.util.LKToastUtil
import com.example.mvp.delegate.BaseAppDelegate
import com.example.mvp.view.ContainsEmojiEditText
import com.example.mvp.view.SKTopBar
import com.example.util.IntegerUtil
import com.leapp.sk.R
import com.leapp.sk.bean.WXUserInfo

/**
 * 微信登录手机号验证视图托管类
 * @author  chaohaipeng  on 2018/8/22.
 */
class LoginMobileVerificationDelegate : BaseAppDelegate(){


    override fun initWidget() {
    }

    override fun initLayoutRes(): Int {
        return R.layout.activity_wxlogin_mobile_verification
    }

    /**
     * 设置清理按钮隐显示
     */
    fun setClearDatavisible(){
        val iv_amv_clear_content= getView<ImageView>(R.id.iv_awmv_clear_content)
        val et_amv_phone= getView<ContainsEmojiEditText>(R.id.et_awmv_phone)
        et_amv_phone.addTextChangedListener(object : TextWatcher {
            override fun afterTextChanged(p0: Editable?) {
                val toString = p0?.toString()
                val compareTo = toString?.length!!.compareTo(0)
                if(compareTo==0){
                    iv_amv_clear_content.visibility= View.INVISIBLE
                }else{
                    iv_amv_clear_content.visibility= View.VISIBLE
                }
            }
            override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
            }
            override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
            }

        })
    }


    /**
     * 设置验证码
     */
    fun setverificationCode(code:String?){
        val et_al_code= getView<ContainsEmojiEditText>(R.id.et_awmv_code)
        if (!TextUtils.isEmpty(code)){
            et_al_code.setText(code)
        }
    }
    /**
     * 登录校验
     */
    fun loginCheck():String{
        val et_amv_code= getView<ContainsEmojiEditText>(R.id.et_awmv_code)
        val codeV=et_amv_code.text.toString().trim()
        if (IntegerUtil.getInstance().isInteger(codeV)&&codeV.length==6){
            return codeV
        }else{
            LKToastUtil.showToastLong(getActivity<AppCompatActivity>().resources.getString(R.string.string_login_toast_check_code))
            return ""
        }

    }
    /**
     * 手机号校验
     */
    fun verificationCodeCheck():String{
        val et_amv_phone= getView<ContainsEmojiEditText>(R.id.et_awmv_phone)
        val tel=et_amv_phone.text.toString().trim()
        if (LKRegexUtil.isMobileExact(tel)){
            return tel
        }else{
            LKToastUtil.showToastLong(getActivity<AppCompatActivity>().resources.getString(R.string.string_login_check_phone))
            return ""
        }

    }


    /**
     * 清理内容
     */
    fun clearContent(){
        val et_amv_phone= getView<ContainsEmojiEditText>(R.id.et_awmv_phone)
        et_amv_phone.setText("")
    }




    /**
     * 验证码
     */
    fun verificationCode(millisUntilFinished:Long){
        val tv_amv_get_code= getView<TextView>(R.id.tv_awmv_get_code)
        if (millisUntilFinished>0){
            tv_amv_get_code.isClickable=false
            tv_amv_get_code.text=(millisUntilFinished / 1000).toString() + " s"
        }else{
            tv_amv_get_code.isClickable=true
            tv_amv_get_code.text=getActivity<AppCompatActivity>().resources.getString(R.string.string_login_again_send)
        }

    }

    /**
     * 设置标题栏
     */
    fun setTitle(title:String?){
        if (!TextUtils.isEmpty(title)){
            val jtb_amv= getView<SKTopBar>(R.id.jtb_awmv)
            jtb_amv.setTitle(title)
        }
        setEvent()
    }

    /**
     * 设置设置事件
     */
    fun setEvent(){
        val jtb_amv= getView<SKTopBar>(R.id.jtb_awmv)
        jtb_amv?.setEventInterface(object : SKTopBar.EventInterface{
            override fun leftOnClick() {
                getActivity<AppCompatActivity>().finish()

            }
            override fun rightOnClick() {
            }

        })
    }

}